/**
 * @name Ccay.example.functionDemo
 * @description role 视图页面控制器
 * 
 * @since 2012-8-15
 * @param page 该参数为控制器对象的内部别名，方便控制器内部的调用。外部访问不到，外部只能通过Ccay.security.role.ListRole访问其方法或属性
 */
Ccay.Module.define("Ccay.example.functionDemo",function(page,$S){
	/**
	 * 单选弹出框页面初始化
	 */
	page.ready=function(){
		$("#roleManageDemo").ccayTab();
		var gridOptions={
				editPermission:"Service$Role$update",
				onRowCheck:selectedRecords,
				columns:[
						 {id: 'chk' ,isCheckColumn : true,frozen:true,align:"center"},
						 {id: 'opration' ,header:"ccay.common.label.operation",frozen:true,width:60,sortable:false,resizable:false,renderer:my_accredit_renderer,ccayPermission:"Service$Security.operation$update"},
					     {id: 'roleName' , header:"ccay.security.label.roleName",editor:{type:"text",validRule:['R',['MAXL',200]]}},
					     {id: 'roleDesc' , header:"ccay.common.label.description",editor:{type:"text",validRule:[['MAXL',2000]]}},
						 {id: 'status' , width:90,header:"ccay.security.role.valid",align:"center",renderer:Ccay.UI.Grid.Renderer.valid,editor:{},format:{type:"valid"}},
						 {id: 'applyStatus' , header:"ccay.security.label.roleApplicationStatus",align:"center", renderer:Ccay.UI.Grid.Renderer.valid,editor:{},format:{type:"valid"}},
						 {id: 'owner' , header:"ccay.common.label.owner",editor:{},format:{type:"jldap"}},
						 {id: 'defaultUrl' , header:"ccay.security.label.roleDefaulturl",editor:{type:"textarea",width:"200px",height:"50px",validRule:[['MAXL',300]]}},
						 {id: 'lastUpdateUserCN', width:140, header:"ccay.common.label.lastUpdatedBy",format:{type:"personInfo"}},
						 {id: 'lastUpdateDate' ,width:140, header:"ccay.common.label.lastUpdatedDate",align:"center",renderer:Ccay.Format.toDateBefore}
						],
				defaultRecord:{roleName:"",roleDesc:"",status:1,applyStatus:1,defaultUrl:""},		
				operation:{
					search:{
						btn:'#btnSearch',
						panel:'#roleCondForm',
						url:'services/ccay/security/role/list/page/{0}/{1}'
					},
					add:{
						btn:'#btnRoleAdd'
					},
					save:{
						btn:'#btnRoleSave',
						fn:function(){
							Ccay.UI.showMessage('"'+$(this.btn).attr("value")+'"：为演示Demo！','question');
						}
					}
				}
		  	}
			$S("#roleGrid").ccayGrid(gridOptions);
		
	}
	
	/**
	 *授权按钮的渲染
	 *hkf16418
	 */
	function my_accredit_renderer(value ,record,columnObj,grid,colNo,rowNo){
			if(record.roleId>0){
				var str = new Array();
				str.push(Ccay.UI.button("ccay.security.grant.rolePermission","Ccay.example.functionDemo.openPermission('{0}')".formatURI(record.roleId)
						,"Service$Security.operation$update","img","key"));
				str.push(Ccay.UI.button("ccay.security.label.viewGroupsAndUsersByRole","Ccay.example.functionDemo.openUsersAndGroups('{0}')".formatURI(record.roleId)
						,"","img","group"));
				return str.join("");
			}else{
			 	return "";
			}	
       } 
	
	/**
	 * 数据行选择
	 */
	function selectedRecords(value,record){
		var datas = $S("#roleGrid").getCcayGridManager().getSelectedRecords();
		if(datas.length != 0){
			$("input[name=replace]").attr('disabled',false)
			$("input[name=replaceAll]").attr("disabled",true);
		}else{
			$("input[name=replace]").attr("disabled",true);
			$("input[name=replaceAll]").attr("disabled",false);
		}
	}
	
	 page.clear = function(){
 		$S("#roleCondForm").nameChild('roleName').ccaySetValue("");
 	 }
     /**
 	 * @description  打开授权Edit页面tab 
 	 * @param roleId 传入的roleId参数
 	 */
     page.openPermission=function(roleId){
    	 Ccay.Common.openPage("ccay.security.grant.rolePermission","ccay/example/functionDemo/roleManage/grantOperation2Role.html",{tab:'#roleManageDemo'}
 		,function(){
 			Ccay.example.functionDemo.GrantOperation2Role.init(roleId);
 		});
     }
     
      /**
 	 * @description  打开查看具有此角色用户和用户组的tab页 
 	 * @param roleId 传入的roleId参数
 	 */
     page.openUsersAndGroups=function(roleId){
    	 Ccay.Common.openPage("ccay.security.label.viewGroupsAndUsersByRole","ccay/example/functionDemo/roleManage/viewUsersAndGroups.html",{tab:'#roleManageDemo'}
 		,function(datas){
 			Ccay.example.functionDemo.ViewUsersAndGroups.init(roleId);
 		});
     }
     
 	/**
 	 * 关闭打开的tab
 	 */
   page.closeTab = function(isReload)
  	{
  		$('#roleManageDemo').ccayTabRemoveItem();
  		if(isReload)
  			$S("#roleGrid").ccayGridSearch();
  	}
	
     page.showMsg = function(type){
     	var name = "";
     	if($S(type).attr("value"))
     		name = $S(type).attr("value");
     	else
     		name = $S(type).attr("title");
     	Ccay.UI.showMessage('"'+name+'"：为演示Demo！','question');
     }
});